package ca.camosun.inventory.gui;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import ca.camosun.inventory.Box;
import ca.camosun.inventory.Box.ItemList;
import ca.camosun.inventory.Item;
import ca.camosun.inventory.R;

public class ViewOneItem extends Activity {

	Item item;
	String itemName;
	String boxName;
	Box aBox;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_one_item);
		
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);

		boxName = getIntent().getExtras().getString("boxName");
		itemName = getIntent().getExtras().getString("itemName");
		aBox = new Box(boxName, this);
		item = aBox.searchForItem(new Item(itemName, itemName, 1));

		EditText editItemName = (EditText) findViewById(R.id.edit_item_name);
		EditText editItemDescription = (EditText) findViewById(R.id.edit_item_description);
		EditText editItemQuantity = (EditText) findViewById(R.id.edit_quantity_field);

		String description = item.getDescription();
		String quantity = Integer.toString(item.getQuantity());

		editItemName.setText(itemName);
		editItemDescription.setText(description);
		editItemQuantity.setText(quantity);

	}

	public void updateItem(View view) {
		Item previousItem = item;
		EditText editItemName = (EditText) findViewById(R.id.edit_item_name);
		EditText editItemDescription = (EditText) findViewById(R.id.edit_item_description);
		EditText editItemQuantity = (EditText) findViewById(R.id.edit_quantity_field);

		itemName = editItemName.getText().toString();
		String itemDesc = editItemDescription.getText().toString();
		int itemQuantity = Integer.parseInt(editItemQuantity.getText()
				.toString());

		item.setName(itemName);
		item.setDescription(itemDesc);
		item.setQuantity(itemQuantity);

		aBox.removeItem(previousItem, this);
		ItemList status = aBox.updateItem(item, previousItem, this);
		
		Toast.makeText(
				this,
				status.toString(),
				Toast.LENGTH_SHORT).show();

		finish();
	}

	public void deleteItem(View view) {
		aBox.removeItem(item, this);
		finish();

	}

	public void cancelItem(View view) {
		finish();

	}

}